﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace INTROSE
{
    public partial class Delete_User : Form
    {
        SqlConnection con = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True");
        private string position, name;
        public Delete_User(string name, string position)
        {
            InitializeComponent();
            this.name = name;
            this.position = position;
            con.Open();
            SqlCommand command = new SqlCommand("select uName from users", con);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    comboUser.Items.Add(reader.GetString(0));
                }
            }
            con.Close();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
            MenuExecutive menu = new MenuExecutive(name, position);
            menu.Show();
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            DialogResult result;
            if (comboUser.Text == "")
                MessageBox.Show("The employee name entered does not exist!");
            else
            {
                result = MessageBox.Show("Are you sure you want to delete?", "Deleting", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    con.Open();
                    SqlCommand command = new SqlCommand("Delete from users where uName = '" + comboUser.Text + "'", con);
                    command.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Account successfully deleted!");
                }
            }
        }

        private void Delete_User_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            if (position == "factory worker")
            {
                MenuProductionStaff menu = new MenuProductionStaff(name, position);
                menu.Show();
            }
            else
            {
                MenuExecutive menu = new MenuExecutive(name, position);
                menu.Show();
            }
        }
    }
}
